﻿using System;
using System.Windows.Forms;

namespace SGLL
{
    public partial class FormComplaints : FormTemplate
    {
        //Global Variables
        private string[] _doctor;
        private string _complaintID;
        //Class Constructor
        public FormComplaints()
        {
            InitializeComponent();
        }
        //Form
        private void FormComplaints_Load(object sender, EventArgs e)
        {
            try
            {
                //Variables
                _doctor = new string[2];
                //Fill out ComboBoxes
                tableAdapterTypeOfSamples.Fill(dataSetComplaints.PROCEDURE_TYPES_OF_SAMPLES_CONSULTATION);
                tableAdapterCloseByUserFilterSpecial.Fill(dataSetComplaints.PROCEDURE_USERS_CONSULTATION_SPECIAL);
                tableAdapterRoutes.Fill(dataSetComplaints.PROCEDURE_ROUTE_CONSULTATION);
                tableAdapterUsers.Fill(dataSetComplaints.PROCEDURE_USERS_CONSULTATION);
                //Setting up the Form
                toolStripButtonClear_Click(null, null);
                //ComboBoxes
                comboBoxOpenByUserFilter.SelectedIndex = comboBoxCloseByUserFilter.SelectedIndex = comboBoxRouteFilter.SelectedIndex = comboBoxDepartamentFilter.SelectedIndex = comboBoxTypeOfSampleFilter.SelectedIndex = -1;
                //Functions
                RefreshComplaints();
            }
            catch (Exception exception)
            {
                ClassFunctions.ErrorLog(exception);
                Close();
            }
        }
        //Label
        private void labelDoctorValue_TextChanged(object sender, EventArgs e)
        {
            EnableButtonSave();
        }
        //Text Boxes
        private void textBoxComments_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !ClassFunctions.TextInputFilter(ClassFunctions.Filters.Alphanumeric, e.KeyChar);
        }
        //Buttons
        private void buttonDoctor_Click(object sender, EventArgs e)
        {
            try
            {
                _doctor = new ClassConsultation("Consulta de Doctores", ClassSQL.GetDoctors).Show();
                labelDoctorValue.Text = _doctor[1];
                
            }
            catch (Exception exception)
            {
                ClassFunctions.ErrorLog(exception);
            }
        }
        //Tool Strip Buttons
        protected override void toolStripButtonSave_Click(object sender, EventArgs e)
        {
            try
            {
                toolStripButtonSave.Enabled = false;
                ClassFunctions.SetData(ClassSQL.SetUpComplaints, new[] { _complaintID, comboBoxTypeOfSample.SelectedValue, comboBoxOpenByUser.Text, _doctor[0], dateTimePickerDate.Value.ToString("s"), textBoxComments.Text.Trim(), comboBoxDepartament.Text, string.IsNullOrEmpty(comboBoxCloseByUser.Text) ? null : comboBoxCloseByUser.Text });
                toolStripButtonClear_Click(null, null);
                RefreshComplaints();
            }
            catch (Exception exception)
            {
                ClassFunctions.ErrorLog(exception);
            }
        }
        protected override void toolStripButtonPrint_Click(object sender, EventArgs e)
        {
            try
            {
                if (!toolStripButtonPrint.Enabled) return;
                toolStripButtonPrint.Enabled = false;
                FormPrintPreview formPrintPreview = new FormPrintPreview
                (
                    dateTimePickerDateFilter.Checked ? dateTimePickerDateFilter.Value : Convert.ToDateTime("1-1-1 0:0:0.0"),
                    comboBoxRouteFilter.SelectedValue == null ? null : comboBoxRouteFilter.Text,
                    comboBoxOpenByUserFilter.SelectedValue == null ? null : comboBoxOpenByUserFilter.Text,
                    comboBoxCloseByUserFilter.SelectedValue == null ? null : comboBoxCloseByUserFilter.Text,
                    comboBoxTypeOfSampleFilter.SelectedValue == null ? Convert.ToByte(0) : Convert.ToByte(comboBoxTypeOfSampleFilter.SelectedValue),
                    string.IsNullOrEmpty(comboBoxDepartamentFilter.Text) ? null : comboBoxDepartamentFilter.Text
                )
                { MdiParent = MdiParent, WindowState = FormWindowState.Maximized };
                formPrintPreview.Show();
            }
            catch (Exception exception)
            {
                ClassFunctions.ErrorLog(exception);
                Close();
            }
        }
        protected override void toolStripButtonClear_Click(object sender, EventArgs e)
        {
            try
            {
                //Variables
                _doctor = new string[3];
                _complaintID = "0";
                //Label
                labelDoctorValue.Text = string.Empty;
                labelIDValue.Text = ClassFunctions.GetSingleData(string.Format(ClassSQL.GetNextID, "[COMPLAINTS]")).ToString();
                //TextBoxes
                textBoxComments.Text = string.Empty;
                //ComboBoxes
                comboBoxCloseByUser.SelectedIndex = comboBoxTypeOfSample.SelectedIndex = comboBoxDepartament.SelectedIndex = -1;
                //DateTime Picker
                dateTimePickerDateFilter.Value = DateTime.Today.Date;
                //Tool Strip Status
                ((FormMain)MdiParent).toolStripStatusLabelCallsValue.Text = ClassFunctions.GetTotalCalls();
                ((FormMain)MdiParent).toolStripStatusLabelLastCallValue.Text = ClassFunctions.GetLastCall();
                //Functions
                EnabledControls(true);
                //Focus
                buttonDoctor.Focus();
            }
            catch (Exception exception)
            {
                ClassFunctions.ErrorLog(exception);
            }
        }
        protected override void toolStripButtonCancel_Click(object sender, EventArgs e)
        {
            FormMain formMain = (FormMain)ParentForm;
            formMain.toolStripMenuItemComplaints.Enabled = true;
            Close();
        }
        //ComboBoxes
        private void comboBoxs_KeyDown(object sender, KeyEventArgs e)
        {
            if (!e.KeyCode.Equals(Keys.Delete)) return;
            ComboBox comboBox = sender as ComboBox;
            comboBox.SelectedIndex = -1;
        }
        private void comboBoxs_SelectionChangeCommitted(object sender, EventArgs e)
        {
            EnableButtonSave();
        }
        private void comboBoxFilters_KeyDown(object sender, KeyEventArgs e)
        {
            if (!e.KeyCode.Equals(Keys.Delete)) return;
            ComboBox comboBox = sender as ComboBox;
            comboBox.SelectedIndex = -1;
            RefreshComplaints();
        }
        private void comboBoxeFilters_SelectionChangeCommitted(object sender, EventArgs e)
        {
            RefreshComplaints();
        }
        //DateTimePicker
        private void dateTimePickerDateFilter_ValueChanged(object sender, EventArgs e)
        {
            RefreshComplaints();
        }
        //Data Grid View
        private void dataGridViewComplaints_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            //Validation
            if (e.RowIndex == -1) return;
            //Coding
            _complaintID = labelIDValue.Text = dataGridViewComplaints[0, e.RowIndex].Value.ToString();
            comboBoxOpenByUser.Text = dataGridViewComplaints[2, e.RowIndex].Value.ToString();
            _doctor[0] = labelDoctorValue.Text = dataGridViewComplaints[3, e.RowIndex].Value.ToString();
            comboBoxTypeOfSample.Text = dataGridViewComplaints[5, e.RowIndex].Value.ToString();
            comboBoxDepartament.Text = dataGridViewComplaints[6, e.RowIndex].Value.ToString();
            textBoxComments.Text = dataGridViewComplaints[7, e.RowIndex].Value.ToString();
            dateTimePickerDate.Value = Convert.ToDateTime(dataGridViewComplaints[8, e.RowIndex].Value);
            //Check if the complaint is still open
            if (Convert.ToBoolean(ClassFunctions.GetSingleData(string.Format(ClassSQL.IsComplaintOpen, dataGridViewComplaints[0, e.RowIndex].Value))))
                comboBoxCloseByUser.SelectedIndex = -1;
            else
                comboBoxCloseByUser.Text = dataGridViewComplaints[9, e.RowIndex].Value.ToString();
            EnabledControls(false);
            EnableButtonSave();
        }
        //General Purpose-Functions
        private void EnableButtonSave()
        {
            toolStripButtonSave.Enabled = comboBoxOpenByUser.SelectedIndex != -1 && !string.IsNullOrEmpty(labelDoctorValue.Text) && !comboBoxDepartament.SelectedIndex.Equals(-1) && !comboBoxTypeOfSample.SelectedIndex.Equals(-1);
        }
        private void EnabledControls(bool value)
        {
            labelID.Enabled =
                labelUser.Enabled =
                comboBoxOpenByUser.Enabled =
                labelDoctor.Enabled =
                buttonDoctor.Enabled =
                labelDepartament.Enabled =
                comboBoxDepartament.Enabled =
                labelTypeOfSample.Enabled =
                comboBoxTypeOfSample.Enabled =
                labelDate.Enabled =
                dateTimePickerDate.Enabled = value;
            //Setting comments and closing user
            labelComments.Enabled = textBoxComments.Enabled = comboBoxCloseByUser.Enabled = comboBoxCloseByUser.SelectedIndex.Equals(-1);
        }
        private void RefreshComplaints()
        {
            try
            {
                //Prevent unwanted automatic selection
                dataGridViewComplaints.RowEnter -= dataGridViewComplaints_RowEnter;
                //Fill Grid with data
                groupBoxComplaints.Text = string.Format("C&asos ({0:##,0})",
                    tableAdapterComplaints.Fill(dataSetComplaints.PROCEDURE_COMPLAINTS_CONSULTATION,
                        dateTimePickerDateFilter.Checked ? dateTimePickerDateFilter.Value : Convert.ToDateTime("1-1-1 0:0:0.0"),
                        comboBoxRouteFilter.SelectedValue == null ? null : comboBoxRouteFilter.Text,
                        comboBoxTypeOfSampleFilter.SelectedValue == null ? Convert.ToByte(0) : Convert.ToByte(comboBoxTypeOfSampleFilter.SelectedValue),
                        comboBoxOpenByUserFilter.SelectedValue == null ? null : comboBoxOpenByUserFilter.Text,
                        comboBoxCloseByUserFilter.SelectedValue == null ? null : comboBoxCloseByUserFilter.Text,
                        string.IsNullOrEmpty(comboBoxDepartamentFilter.Text) ? null : comboBoxDepartamentFilter.Text
                    ));
                //Clear Selection
                dataGridViewComplaints.ClearSelection();
                //Set up Group Box
                groupBoxComplaints.Text = string.Format("Ll&amadas ({0:##,0})", dataGridViewComplaints.Rows.Count);
                //Enabled Buttons
                toolStripButtonPrint.Enabled = !dataGridViewComplaints.Rows.Count.Equals(0);
                //Restore Method
                dataGridViewComplaints.RowEnter += dataGridViewComplaints_RowEnter;
            }
            catch (Exception exception)
            {
                ClassFunctions.ErrorLog(exception);
            }
        }
    }
}